package com.mango.ch08;

import java.util.Set;
import java.util.TreeSet;


public class CommonFriends  {
	
	public static Set<Integer> intersection(Set<Integer> user1Friends, Set<Integer> user2Friends) {
		if ((user1Friends == null) || user1Friends.isEmpty()) {
			return null;
		}
		if ((user2Friends == null) || user2Friends.isEmpty()) {
			return null;
		}
		// 两个集合都不是空集
		if (user1Friends.size() < user2Friends.size()) {
			return intersect(user1Friends, user2Friends);
		} else {
			return intersect(user2Friends, user1Friends);
		}
	}

	public static Set<Integer> intersect(Set<Integer> small, Set<Integer> large) {
		Set<Integer> result = new TreeSet<>();
		// 迭代处理集合 提高性能
		for (Integer integer : small) {
			if (large.contains(integer))
				result.add(integer);
		}
		return result;
	}


}
